import { postApiDesignerPlatformUpload } from '@/api/designer/designer'
import { entryQuery } from '@/definitions/component-definition'
import { maxFileSize } from '@/utils/constants'
import { sizeLimit } from '@/utils/file'
import { message } from 'ant-design-vue'

export interface UploadResult {
  isImg: boolean
  /** 文件字节大小 */
  length: number
  /** 上传文件原始名(含后缀) */
  name: string
  /** 上传文件可用完整路径 */
  url: string
}
export function uploadFile(file: File) {
  if (!sizeLimit(file.size, maxFileSize)) {
    const errMsg = `文件大小不能超过${maxFileSize / 1024 / 1024}MB`
    message.error(errMsg)
    return Promise.reject(errMsg)
  }
  const fd = new FormData()
  fd.append('file', file)
  return postApiDesignerPlatformUpload(entryQuery.platform!, fd) as Promise<UploadResult>
}
